<?php
/**
 * CreateTime: 2023/1/10 15:30
 */

namespace App\Models\MeLawCase;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\MeLawCaseConsts\MeLawCaseConsts;
use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModelV2;
use App\Models\Department\DepartmentModel;

class MeLawCase extends BaseModelV2
{
    protected $table = TableConsts::ME_LAW_CASE;

    protected $primaryKey = 'law_case_id';

    protected $fillable = [
        'respondent_name',
        'respondent_idcard',
        'respondent_phone',
        'respondent_house_address',
        'respondent_address',
        'respondent_bank',
        'respondent_bank_number',
        'respondent_unit_name',
        'respondent_unit_address',
        'respondent_remark',
        'law_case_code',
//        'law_case_start_time',
//        'law_case_end_time',
        'law_case_status',
        'loans_name',
        'loans_contract_code',
        'loans_time',
        'loans_start_time',
        'loans_money',
        'loans_output_money_principal',
        'loans_output_money_interest',
        'loans_input_money_principal',
        'loans_input_money_interest',
        'loans_overdue_time',
        'loans_remark',
        'loans_overdue_fine',
        'loans_interest_penalty',
        'loans_periods_number',
        'loans_periods_input_number',
        'created_at',
        'updated_at',
        'status',
        'owner_id',
        'seats_id',
        'start_mediator_time',
        'law_case_latter_custom',
        'law_case_custom',
        'seal_file_ids',
        'main_respondent_phone_id',
        'is_send_law_latter',
        'is_send_latter',
        'law_case_batch_number',
        'law_case_ex'
    ];

//    protected $casts = [
//        'respondent_id_card' => 'string',
//        'respondent_bank_number' => 'string'
//    ];

    public function getList($query,$pageSize): mixed
    {
        return $query->where('law_case.status',BaseConsts::STATUS_NORMAL)
            ->latest()
            ->Paginate($pageSize);
    }

    public function bindMediator(mixed $mediator_id, mixed $law_case_list)
    {
        return $this->whereIn('law_case_id',$law_case_list)
            ->where('law_case_status',MeLawCaseConsts::ME_LAW_CASE_DFP_1)
            ->where('status',1)
            ->update([
                'mediator_id' => $mediator_id,
                'law_case_status' => MeLawCaseConsts::ME_LAW_CASE_YFP_2
            ]);
    }

    public function updateLawCaseByIds(array $law_case_list, $update)
    {
        return $this->whereIn('law_case_id',$law_case_list)->where('status',1)->update($update);
    }


    public function getLawCaseByIds(array $law_case_list,$select = ['law_case_code','law_case_id','mediator_id'])
    {
        return $this
            ->select($select)
            ->whereIn('law_case_id',$law_case_list)
            ->where('status',1)
            ->get();
    }

    public function getStartMediatorTimeAttribute($value){
        return date('Y-m-d',strtotime($value));
    }

    public function getDepartmentStrAttribute($value){
        $departModel = new DepartmentModel();
        return $departModel->getById($value,['department_name'])['department_name'] ?? "-";
    }

    public function updateByIds($ids,$data){
        return $this->whereIn('law_case_id',$ids)->update($data);
    }


}